package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.NewsAddReq;
import com.zucc.controller.req.NewsModifyReq;
import com.zucc.controller.req.NewsSearchReq;
import com.zucc.controller.resp.NewsResp;
import com.zucc.entity.News;
import com.zucc.entity.NewsMapper;
import com.zucc.entity.Topic;
import com.zucc.entity.TopicMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.NewsService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-11 4:47 下午
 */
@Service
public class NewsServiceImpl extends MyServiceImpl<News> implements NewsService {
    @Resource
    private TopicMapper topicMapper;

    @Resource
    private NewsMapper newsMapper;

    @Override
    public MyMapper<News> mapper() {
        return newsMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(NewsAddReq req) {
        if (Objects.nonNull(req.getTopicId()) && req.getTopicId().longValue() != 0L) {
            Topic topic = topicMapper.selectByPrimaryKey(req.getTopicId());
            if (Objects.isNull(topic) || topic.getDelFlag().intValue() == 1) {
                throw new SyzxException("系统找不到相应编号的栏目信息");
            }
        }
        News news = new News();
        BeanUtil.copyProperties(req, news);
        news.setCount(0);
        news.setDelFlag(0);
        news.setCreateTime(DateUtil.date());
        news.setModifyTime(DateUtil.date());
        super.saveKey(news);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(NewsModifyReq req) {
        News newsExist = super.findById(req.getId());
        if (Objects.isNull(newsExist) || newsExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的新闻信息");
        }
        BeanUtil.copyProperties(req, newsExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        newsExist.setModifyTime(DateUtil.date());
        super.updateSelective(newsExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        News newsExist = super.findById(id);
        if (Objects.isNull(newsExist) || newsExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的新闻信息");
        }
        newsExist.setDelFlag(1);
        newsExist.setDeleteTime(DateUtil.date());
        super.updateSelective(newsExist);
    }

    @Override
    public PageData<NewsResp> page(NewsSearchReq req) {
        Example example = new Example(News.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        if (Objects.nonNull(req.getTopicId())) {
            criteria.andEqualTo("topicId", req.getTopicId());
        }
        if (StrUtil.isNotBlank(req.getTitle())) {
            criteria.andLike("title", "%" + req.getTitle() + "%");
        }
        if (Objects.nonNull(req.getHot())) {
            criteria.andEqualTo("hot", req.getHot());
        }
        if (StrUtil.isNotBlank(req.getContent())) {
            criteria.andLike("content", "%" + req.getContent() + "%");
        }
        if (StrUtil.isNotBlank(req.getRemarks())) {
            criteria.andLike("remarks", "%" + req.getRemarks() + "%");
        }
        PageData<News> pageData = super.findByPage(req, example);
        PageData<NewsResp> convert = MyPageUtil.convert(pageData, NewsResp.class);
        if (convert.getList().isEmpty()) {
            return convert;
        }
        List<Long> topicIds = convert.getList().stream().filter(i -> i.getTopicId() != null && i.getTopicId().longValue() != 0L).map(NewsResp::getTopicId).distinct().collect(Collectors.toList());
        if (topicIds.isEmpty()) {
            return convert;
        }
        List<Topic> list = new ArrayList<>();
        for (Long topicId : topicIds) {
            list.add(topicMapper.selectByPrimaryKey(topicId));
        }
        Map<Long, String> topicMap = list.stream().collect(Collectors.toMap(Topic::getId, Topic::getName));
        for (NewsResp newsResp : convert.getList()) {
            newsResp.setTopicName(topicMap.get(newsResp.getTopicId()));
        }
        return convert;
    }
}
